home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / p_man / cat3 / Thread.Z / Thread
Encoding:
Text File  |  1998-10-28  |  7.4 KB  |  265 lines

  1.  
  2.  
  3.  
  4.      TTTThhhhrrrreeeeaaaadddd((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         TTTThhhhrrrreeeeaaaadddd((((3333))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       Thread - multithreading
  10.  
  11.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.           use Thread;
  13.  
  14.           my $t = new Thread \&start_sub, @start_args;
  15.  
  16.           $t->join;
  17.  
  18.           my $tid =    Thread->self->tid;
  19.  
  20.           my $tlist    = Thread->list;
  21.  
  22.           lock($scalar);
  23.  
  24.           use Thread 'async';
  25.  
  26.           use Thread 'eval';
  27.  
  28.  
  29.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  30.       The Thread module provides multithreading support for    perl.
  31.  
  32.      FFFFUUUUNNNNCCCCTTTTIIIIOOOONNNNSSSS
  33.       new \&start_sub
  34.  
  35.       new \&start_sub, LIST
  36.           new starts a new thread of execution in the
  37.           referenced subroutine. The optional list is passed
  38.           as parameters    to the subroutine. Execution continues
  39.           in both the subroutine and the code after the    new
  40.           call.
  41.  
  42.           new Thread returns a thread object representing the
  43.           newly    created    thread.
  44.  
  45.       lock VARIABLE
  46.           lock places a    lock on    a variable until the lock goes
  47.           out of scope.     If the    variable is locked by another
  48.           thread, the lock call    will block until it's
  49.           available. lock is recursive,    so multiple calls to
  50.           lock are safe--the variable will remain locked until
  51.           the outermost    lock on    the variable goes out of
  52.           scope.
  53.  
  54.           Locks    on variables only affect lock calls--they do
  55.           _n_o_t affect normal access to a    variable. (Locks on
  56.           subs are different, and covered in a bit) If you
  57.           really, _r_e_a_l_l_y want locks to block access, then go
  58.           ahead    and tie    them to    something and manage this
  59.           yourself. This is done on purpose. While managing
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      TTTThhhhrrrreeeeaaaadddd((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         TTTThhhhrrrreeeeaaaadddd((((3333))))
  71.  
  72.  
  73.  
  74.           access to variables is a good    thing, perl doesn't
  75.           force    you out    of its living room...
  76.  
  77.           If a container object, such as a hash    or array, is
  78.           locked, all the elements of that container are not
  79.           locked. For example, if a thread does    a lock @a, any
  80.           other    thread doing a lock($a[12]) won't block.
  81.  
  82.           You may also lock a sub, using lock &sub. Any    calls
  83.           to that sub from another thread will block until the
  84.           lock is released. This behaviour is not equvalent to
  85.           use attrs qw(locked) in the sub. use attrs
  86.           qw(locked) serializes    access to a subroutine,    but
  87.           allows different threads non-simultaneous access.
  88.           lock &sub, on    the other hand,    will not allow _a_n_y
  89.           other    thread access for the duration of the lock.
  90.  
  91.           Finally, lock    will traverse up references exactly
  92.           _o_n_e level.  lock(\$a)    is equivalent to lock($a),
  93.           while    lock(\\$a) is not.
  94.  
  95.       async    BLOCK;
  96.           async    creates    a thread to execute the    block
  97.           immediately following    it. This block is treated as
  98.           an anonymous sub, and    so must    have a semi-colon
  99.           after    the closing brace. Like    new Thread, async
  100.           returns a thread object.
  101.  
  102.       Thread->self
  103.           The Thread->self function returns a thread object
  104.           that represents the thread making the    Thread->self
  105.           call.
  106.  
  107.       Thread->list
  108.           Thread->list returns a list of thread    objects    for
  109.           all running and finished but un-joined threads.
  110.  
  111.       cond_wait VARIABLE
  112.           The cond_wait    function takes a lllloooocccckkkkeeeedddd    variable as a
  113.           parameter, unlocks the variable, and blocks until
  114.           another thread does a    cond_signal or cond_broadcast
  115.           for that same    locked variable. The variable that
  116.           cond_wait blocked on is relocked after the cond_wait
  117.           is satisfied.     If there are multiple threads
  118.           cond_waiting on the same variable, all but one will
  119.           reblock waiting to reaquire the lock on the
  120.           variable. (So    if you're only using cond_wait for
  121.           synchronization, give    up the lock as soon as
  122.           possible)
  123.  
  124.       cond_signal VARIABLE
  125.           The cond_signal function takes a locked variable as
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      TTTThhhhrrrreeeeaaaadddd((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         TTTThhhhrrrreeeeaaaadddd((((3333))))
  137.  
  138.  
  139.  
  140.           a parameter and unblocks one thread that's
  141.           cond_waiting on that variable. If more than one
  142.           thread is blocked in a cond_wait on that variable,
  143.           only one (and    which one is indeterminate) will be
  144.           unblocked.
  145.  
  146.           If there are no threads blocked in a cond_wait on
  147.           the variable,    the signal is discarded.
  148.  
  149.       cond_broadcast VARIABLE
  150.           The cond_broadcast function works similarly to
  151.           cond_wait.  cond_broadcast, though, will unblock aaaallllllll
  152.           the threads that are blocked in a cond_wait on the
  153.           locked variable, rather than only one.
  154.  
  155.      MMMMEEEETTTTHHHHOOOODDDDSSSS
  156.       join      join waits for a thread to end and returns any
  157.           values the thread exited with. join will block until
  158.           the thread has ended,    though it won't    block if the
  159.           thread has already terminated.
  160.  
  161.           If the thread    being joined died, the error it    died
  162.           with will be returned    at this    time. If you don't
  163.           want the thread performing the join to die as    well,
  164.           you should either wrap the join in an    eval or    use
  165.           the eval thread method instead of join.
  166.  
  167.       eval      The eval method wraps    an eval    around a join, and so
  168.           waits    for a thread to    exit, passing along any    values
  169.           the thread might have    returned.  Errors, of course,
  170.           get placed into $@.
  171.  
  172.       tid      The tid method returns the tid of a thread. The tid
  173.           is a monotonically increasing    integer    assigned when
  174.           a thread is created. The main    thread of a program
  175.           will have a tid of zero, while subsequent threads
  176.           will have tids assigned starting with    one.
  177.  
  178.      LLLLIIIIMMMMIIIITTTTAAAATTTTIIIIOOOONNNNSSSS
  179.       The sequence number used to assign tids is a simple integer,
  180.       and no checking is done to make sure the tid isn't currently
  181.       in use. If a program creates more than 2^32 -    1 threads in a
  182.       single run, threads may be assigned duplicate    tids. This
  183.       limitation may be lifted in a    future version of Perl.
  184.  
  185.      SSSSEEEEEEEE AAAALLLLSSSSOOOO
  186.       the _a_t_t_r_s manpage, the _T_h_r_e_a_d::_Q_u_e_u_e manpage,    the
  187.       _T_h_r_e_a_d::_S_e_m_a_p_h_o_r_e manpage, the _T_h_r_e_a_d::_S_p_e_c_i_f_i_c manpage.
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      TTTThhhhrrrreeeeaaaadddd((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         TTTThhhhrrrreeeeaaaadddd((((3333))))
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.      Page 4                        (printed 10/23/98)
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.